import { message } from 'antd';
import {
  getEmpList,
  probEmpList,
  empTabNum,
  cancelChangeProt,
  getDownloadInfo,
  getUploadProgress,
  getProDetail,
  getFeedback,
  getQuerycity,
  getEmpSeleInfo,
  platformSupplier,
  contractpartyList,
  custrnameList,
  getBusinessList,
  getQuotationList,
  gethetionList,
  getCustmorList,
  pleaseListqun,
  costCenterList,
  personnelStatusList,
  getPartyList,
  agreeWithone,
  providentRatioList,
  providentFundList,
  accountPolicy,
  serviceConfiguraList,
  laborContract,
  isStafffaction,
  addAlert,
  upTipsalert,
  subMessage,
  dictionariesList,
} from '@/services/adjusttasksend';

export default {
  state: {
    activeKey: '2',
    tabList: [
      { itemCode: '2', itemName: '已发单', isHasBadge: false },
      { itemCode: '3', itemName: '已接单', isHasBadge: false },
      { itemCode: '4', itemName: '已回单', isHasBadge: false },
      { itemCode: '5', itemName: '问题人员', isHasBadge: false },
      { itemCode: '6', itemName: '已取消', isHasBadge: false },
    ],
    formFieldsValue: {},
    expandForm: false,
    editFlag: 'null',
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    batchQuery: {
      // 批量上传数据
      sendType: '1',
      isSerSetFlag: '1',
      progressPercent: 0,
    },
    dictionList: {},
    proBaseList: {},
    proFbList: [],
    probId: '',
  },

  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'updateList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取问题人员列表
    *fetchProbList({ payload }, { call, put }) {
      const response = yield call(probEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'updateList',
        payload: {
          data: {},
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取页签数量
    *fetchTabNum({ payload }, { call, put, select }) {
      const response = yield call(empTabNum, payload.formFieldsValue);
      let activeKey = yield select(state => state.adjusttasksend.activeKey);
      const tabList = yield select(state => state.adjusttasksend.tabList);
      if (response.success) {
        if (response.data && response.data.length) {
          const sortNumber = (a, b) => a.itemCode - b.itemCode;
          const tabCodeArr = response.data.sort(sortNumber);
          const isHasBadgeCurrentTab = tabCodeArr.some(item => `${item.tabHdl}` === activeKey); // 当前页签有没有查询到
          if (!isHasBadgeCurrentTab) {
            activeKey = `${tabCodeArr[0].tabHdl}`;
          }
          tabCodeArr.forEach(item => {
            if (`${item.tabHdl}` !== activeKey) {
              tabList[item.tabHdl - 2].isHasBadge = true;
            }
          });
          yield put({
            type: 'updateModelData',
            payload: { activeKey, tabList },
          });
          const getListParams = {
            ...payload.formFieldsValue,
            pageArg: { currentPage: payload.currentPage, pageSize: payload.pageSize },
          };
          if (activeKey === '5') {
            if (payload.formFieldsValue) {
              yield put({
                type: 'updateModelData',
                payload: {
                  ...payload.formFieldsValue,
                },
              });
            }
            yield put({
              type: 'problempage/getProList',
              payload: {
                ...getListParams,
                busiType: '11',
              },
            });
          } else {
            yield put({
              type: 'fetchList',
              payload: { ...getListParams, hdlState: activeKey },
            });
          }
        } else {
          yield put({
            type: 'updateList',
            payload: {
              data: {},
              currentPage: 1,
              pageSize: 10,
            },
          });
          if (activeKey === '5') {
            yield put({
              type: 'problempage/updateProList',
              payload: {
                data: [],
                currentPage: 1,
                pageSize: 10,
              },
            });
          }
        }
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 取消更换协议
    *cancelChangeProt({ payload }, { call }) {
      const response = yield call(cancelChangeProt, payload);
      return response;
    },

    // 获取下载模板fileId fileName
    *getDownloadInfo({ payload }, { call, put }) {
      const response = yield call(getDownloadInfo, payload);
      yield put({
        type: 'updateQuery',
        payload,
      });
      if (response.success) {
        const fileInfo = response.data.data.split('|');
        yield put({
          type: 'publicdata/setFileDownload',
          payload: { fileId: fileInfo[0], fileName: fileInfo[1] },
        });
      }
    },
    // 获取上传进度数据
    *getUploadProgress({ payload, callback }, { call, put }) {
      const response = yield call(getUploadProgress, payload);
      if (response.success) {
        yield put({
          type: 'updateQuery',
          payload: { progressPercent: response.data.successProp },
        });
      }
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },

    // 获取问题人员详情
    *fetchProDetailList({ payload }, { call, put }) {
      const response = yield call(getProDetail, payload);
      if (response.success) {
        yield put({
          type: 'getProDetail',
          payload: response.data,
        });
      }
    },
    // 问题人员反馈
    *proFeedBack({ payload, callback }, { call }) {
      const response = yield call(getFeedback, payload);
      if (response.success) {
        if (callback) {
          callback(response);
        }
      } else {
        message.warning(`${response.message || '反馈失败'}`);
      }
    },
    // 问题人员反馈并取消标记
    *cancelProbFlag({ payload, callback }, { call }) {
      const response = yield call(getFeedback, payload);
      if (callback) {
        callback(response);
      }
    },

    // *************************************************************************** ylq
    // 查询市
    *querycity({ payload, callback }, { call }) {
      const response = yield call(getQuerycity, payload);
      if (callback) {
        callback(response);
      }
    },
    // 获取当前角色所在公司对应的平台供应商
    *plasuppList({ payload, callback }, { call }) {
      const response = yield call(platformSupplier, payload);
      if (callback) {
        callback(response);
      }
    },

    // 签约方列表
    *contractParty({ payload, callback }, { call }) {
      const response = yield call(contractpartyList, payload);
      if (callback) {
        callback(response);
      }
    },

    // 客户名称列表
    *customerList({ payload, callback }, { call }) {
      const response = yield call(custrnameList, payload);
      if (callback) {
        callback(response);
      }
    },
    // 获取字典
    *fetchDictCode({ payload, callback }, { call }) {
      const response = yield call(dictionariesList, payload);
      if (response.success) {
        if (callback) {
          callback(response.data);
        }
      }
    },

    // 选择客户名称：请求商务合同
    *getBusiness({ payload, callback }, { call }) {
      const response = yield call(getBusinessList, payload);
      if (callback) {
        callback(response);
      }
    },

    // 选择商务合同：请求报价单
    *getQuotation({ payload, callback }, { call }) {
      const response = yield call(getQuotationList, payload);
      if (callback) {
        callback(response);
      }
    },

    // 根据签约方为全国：请求供应商合同
    *getNameList({ payload, callback }, { call }) {
      const response = yield call(gethetionList, payload);
      if (callback) {
        callback(response);
      }
    },

    // 根据选择的供应商合同：请求客户名称
    *getCustnameList({ payload, callback }, { call }) {
      const response = yield call(getCustmorList, payload);
      if (callback) {
        callback(response);
      }
    },

    *subaddMessage({ payload, callback }, { call }) {
      const response = yield call(subMessage, payload);
      if (callback) {
        callback(response);
      }
    },

    // 选择客户名称：请求报价单
    *pleaseQun({ payload, callback }, { call }) {
      const response = yield call(pleaseListqun, payload);
      if (callback) {
        callback(response);
      }
    },

    // 根据报价单id获取产品等信息
    *crmquoGet({ payload, callback }, { call }) {
      const response = yield call(getEmpSeleInfo, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 所属成本中心
    *costCenter({ payload, callback }, { call }) {
      const response = yield call(costCenterList, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 获取缴费人员类别（参保身份)
    *personnelCategory({ payload, callback }, { call }) {
      const response = yield call(personnelStatusList, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 根据执行地调出执行方
    *executiveParty({ payload, callback }, { call }) {
      const response = yield call(getPartyList, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 根据执行地查询五险合不合一
    *combinationorOne({ payload, callback }, { call }) {
      const response = yield call(agreeWithone, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 提交时的提示弹框
    *tipsalert({ payload, callback }, { call }) {
      const response = yield call(upTipsalert, payload);
      callback(response);
    },

    // 获取公积金比例下拉列表
    *providentRatio({ payload, callback }, { call }) {
      const response = yield call(providentRatioList, payload);
      if (response.success) {
        callback(response);
      }
    },

    //
    *providentFund({ payload, callback }, { call }) {
      const response = yield call(providentFundList, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 根据公积金比例账户政策组合
    *policyMix({ payload, callback }, { call }) {
      const response = yield call(accountPolicy, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 根据执行地查询服务配置
    *serviceConfiguration({ payload, callback }, { call }) {
      const response = yield call(serviceConfiguraList, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 获取劳动合同信息
    *contractInformation({ payload, callback }, { call }) {
      const response = yield call(laborContract, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 判断是否员工多派
    *submissionMessage({ payload, callback }, { call }) {
      const response = yield call(isStafffaction, payload);
      if (response.success) {
        callback(response);
      }
    },

    // 增员弹出提示框
    *alertTips({ payload, callback }, { call }) {
      const response = yield call(addAlert, payload);
      if (response.success) {
        callback(response);
      }
    },
  },

  reducers: {
    // 获取列表
    updateList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    // 更新批量上传参数
    updateQuery(state, { payload }) {
      return {
        ...state,
        batchQuery: { ...state.batchQuery, ...payload },
      };
    },

    dictionList(state, { payload }) {
      return {
        ...state,
        dictionList: { ...payload },
      };
    },
    // 问题人员详情
    getProDetail(state, { payload }) {
      return {
        ...state,
        proBaseList: payload,
        proFbList: payload.probFdbkList,
        probId: payload.probId,
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 设置页签状态
    updateTabListQuery(state, { payload }) {
      const { tabList } = state;
      tabList.forEach((item, index) => {
        if (item.itemCode === payload.activeKey) {
          tabList[index].isHasBadge = false;
        }
      });
      return {
        ...state,
        tabList,
        ...payload,
      };
    },
  },
};
